﻿#include <occmesh/mesh/ais_mesh.h>

IMPLEMENT_STANDARD_RTTIEXT(AIS_Mesh, AIS_Triangulation)

AIS_Mesh::AIS_Mesh(const Handle(Poly_Triangulation) & Triangulation) : AIS_Triangulation(Triangulation)
{
}

void AIS_Mesh::Compute(const Handle(PrsMgr_PresentationManager) &, const Handle(Prs3d_Presentation) & thePrs,
                       const Standard_Integer theMode)
{
    if (theMode != 0)
    {
        return;
    }

    auto myTriangulation = GetTriangulation();
    auto myNbNodes = myTriangulation->NbNodes();
    auto myNbTriangles = myTriangulation->NbTriangles();
    auto myColor = GetColors();

    Standard_Boolean hasVNormals = myTriangulation->HasNormals();
    Standard_Boolean hasVColors = HasVertexColors();

    Handle(Graphic3d_ArrayOfTriangles) anArray =
        new Graphic3d_ArrayOfTriangles(myNbNodes, myNbTriangles * 3, hasVNormals, hasVColors, Standard_False);
    Handle(Graphic3d_Group) aGroup = thePrs->CurrentGroup();
    Handle(Graphic3d_AspectFillArea3d) anAspect = myDrawer->ShadingAspect()->Aspect();
    
    const Standard_Real anAmbient = 1.0;
    if (!hasVNormals)
        myTriangulation->ComputeNormals();

    gp_Vec3f aNormal;
    for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter)
    {
        anArray->AddVertex(myTriangulation->Node(aNodeIter));
        myTriangulation->Normal(aNodeIter, aNormal);
        anArray->SetVertexNormal(aNodeIter, aNormal.x(), aNormal.y(), aNormal.z());
    }

    Standard_Integer aTriIndices[3] = {0, 0, 0};
    for (Standard_Integer aTriIter = 1; aTriIter <= myTriangulation->NbTriangles(); ++aTriIter)
    {
        myTriangulation->Triangle(aTriIter).Get(aTriIndices[0], aTriIndices[1], aTriIndices[2]);
        anArray->AddEdge(aTriIndices[0]);
        anArray->AddEdge(aTriIndices[1]);
        anArray->AddEdge(aTriIndices[2]);
    }
    aGroup->SetPrimitivesAspect(anAspect);
    aGroup->AddPrimitiveArray(anArray);
}

void AIS_Mesh::ComputeSelection(const Handle(SelectMgr_Selection) & /*aSelection*/, const Standard_Integer /*aMode*/)
{
}

void AIS_Mesh::SetSelectionColor(float r, float g, float b, float a)
{

}